package com.ruoyi.lxy.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.lxy.domain.TUser;
import com.ruoyi.lxy.domain.vo.TDependantVo;
import com.ruoyi.lxy.domain.vo.TRegistrationVo;
import com.ruoyi.lxy.domain.vo.TRosterVo;
import com.ruoyi.lxy.service.ITDependantService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TUserVo;
import com.ruoyi.lxy.domain.bo.TUserBo;
import com.ruoyi.lxy.service.ITUserService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lxy/user")
public class TUserController extends BaseController {

    private final ITUserService iTUserService;
    private final ITDependantService iTDependantService;

    /**
     * 查询用户列表
     */
    @SaCheckPermission("lxy:user:list")
    @GetMapping("/list")
    public TableDataInfo<TUserVo> list(TUserBo bo, PageQuery pageQuery) {
        return iTUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户列表
     */
    @SaCheckPermission("lxy:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{timeId}")
    public void export(@PathVariable Long timeId,HttpServletResponse response) {
        List<TRosterVo> RosterList = iTUserService.queryList(timeId);
        ExcelUtil.exportExcel(RosterList, "旅游名单", TRosterVo.class, response);
    }

    /**
     * 获取用户详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:user:query")
    @GetMapping("/{id}")
    public R<TUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTUserService.queryById(id));
    }

    /**
     * 新增用户
     */
    @SaCheckPermission("lxy:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TUserBo bo) {
        return toAjax(iTUserService.insertByBo(bo));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("lxy:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TUserBo bo) {
        return toAjax(iTUserService.updateByBo(bo));
    }

    /**
     * 删除用户
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iTUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }




    @SaCheckPermission("lxy:user:list")
    @GetMapping("/list/{timeId}")
    public TableDataInfo<TRosterVo> selectUserListByTimeId(@PathVariable Long timeId, PageQuery pageQuery) {
        return iTUserService.selectUserListByTimeId(timeId,pageQuery);
    }

}
